package com.keldian.myqq.tabhost;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.keldian.myqq.R;
import com.keldian.myqq.net.HttpGetData;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyQQNews extends Activity implements View.OnClickListener {

    @ViewInject(R.id.button)
    private Button button;
    List<Map<String, String>> moreList;
    private PopupWindow popupWindow;
    @ViewInject(R.id.lv_popup_list)
    private ListView lvPopupList;
    private int NUM_OF_VISIBLE_LIST_ROWS = 3;

    @ViewInject(R.id.listview)
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_item_news);
        ViewUtils.inject(this);

        initData();
        initPopupWindow();
        button.setOnClickListener(this);
    }

    private void initData() {
        moreList = new ArrayList<Map<String, String>>();
        Map<String, String> map;
        map = new HashMap<String, String>();
        map.put("share_key", "复制");
        moreList.add(map);
        map = new HashMap<String, String>();
        map.put("share_key", "删除");
        moreList.add(map);
        map = new HashMap<String, String>();
        map.put("share_key", "修改");
        moreList.add(map);

    }

    private void initPopupWindow() {
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.task_detail_popupwindow, null);
        ViewUtils.inject(layout);
        popupWindow = new PopupWindow();

        popupWindow.setFocusable(true);
        lvPopupList.setAdapter(new SimpleAdapter(MyQQNews.this,moreList,
                R.layout.list_item_popupwindow, new String[]{"share_key"},
                new int[]{R.id.tv_list_item}));

        lvPopupList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(MyQQNews.this, MyQQNewMessage.class);
                startActivity(intent);

            }
        });

        lvPopupList.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        popupWindow.setWidth(lvPopupList.getMeasuredWidth());
        popupWindow.setHeight((lvPopupList.getDividerHeight()) * NUM_OF_VISIBLE_LIST_ROWS);

        popupWindow.setBackgroundDrawable(this.getResources().getDrawable
                (R.drawable.bg_popupwindow));

        popupWindow.setOutsideTouchable(true);

    }

    @OnClick(R.id.button)
    public void onClick(View v) {
        if(popupWindow.isShowing()){
            popupWindow.dismiss();
        }else{
            popupWindow.showAsDropDown(button);
        }
    }

}
